package com.ruoyi.park.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.park.domain.BuiBuilding;
import com.ruoyi.park.service.IBuiBuildingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 楼栋Controller
 *
 * @author cjie
 * @date 2021-07-20
 */
@Api(tags = " 楼栋")
@RestController
@RequestMapping("/buiBuilding")
public class BuiBuildingController extends BaseController {
    @Autowired
    private IBuiBuildingService buiBuildingService;


    @PostMapping("/getBuildingByCode")
    public R getBuildingByCode(@RequestParam(value = "parkCode", required = false) String parkCode, @RequestParam(value = "buildingCode", required = false) String buildingCode) {
        BuiBuilding query = new BuiBuilding();
        query.setBuildingNum(buildingCode);
        query.setParkNum(parkCode);
        List<BuiBuilding> list = buiBuildingService.selectBuiBuildingList(query);
        if (list.size() > 0) {
            return R.ok(list.get(0));
        }
        return R.fail();
    }


    /**
     * 查询楼栋列表
     */
    @ApiOperation(value = "查询楼栋列表", notes = "查询楼栋列表")
    @PreAuthorize(hasPermi = "park:buiBuilding:list")
    @GetMapping("/list")
    public TableDataInfo list(BuiBuilding buiBuilding) {
        startPage();
        List<BuiBuilding> list = buiBuildingService.selectBuiBuildingList(buiBuilding);
        return getDataTable(list);
    }

    /**
     * 导出楼栋列表
     */
    @ApiOperation(value = "导出楼栋列表", notes = "导出楼栋列表")
    @PreAuthorize(hasPermi = "park:buiBuilding:export")
    @Log(title = "楼栋", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuiBuilding buiBuilding) throws IOException {
        List<BuiBuilding> list = buiBuildingService.selectBuiBuildingList(buiBuilding);
        ExcelUtil<BuiBuilding> util = new ExcelUtil<BuiBuilding>(BuiBuilding.class);
        util.exportExcel(response, list, "楼栋数据");
    }

    /**
     * 获取楼栋详细信息
     */
    @ApiOperation(value = "获取楼栋详细信息", notes = "获取楼栋详细信息")
    @PreAuthorize(hasPermi = "park:buiBuilding:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(buiBuildingService.selectBuiBuildingById(id));
    }

    /**
     * 新增楼栋
     */
    @ApiOperation(value = "新增楼栋", notes = "新增楼栋")
    @PreAuthorize(hasPermi = "park:buiBuilding:add")
    @Log(title = "楼栋", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuiBuilding buiBuilding) {
        return buiBuildingService.insertBuiBuilding(buiBuilding);
    }

    /**
     * 修改楼栋
     */
    @ApiOperation(value = "修改楼栋", notes = "修改楼栋")
    @PreAuthorize(hasPermi = "park:buiBuilding:edit")
    @Log(title = "楼栋", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuiBuilding buiBuilding) {
        return buiBuildingService.updateBuiBuilding(buiBuilding);
    }

    /**
     * 删除楼栋
     */
    @ApiOperation(value = "删除楼栋", notes = "删除楼栋")
    @ApiImplicitParam(name = "ids", value = "楼栋ID数组", required = true, paramType = "path", allowMultiple = true, dataType = "String")
    @PreAuthorize(hasPermi = "park:buiBuilding:remove")
    @Log(title = "楼栋", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return buiBuildingService.deleteBuiBuildingByIds(ids);
    }


}
